package com.rrd.dw.mr.gzip.kafka;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.ypcredit.TcreditUnitedScoreData;
import com.ucredit.babel.common.model.third.ypcredit.TcreditUnitedScoreRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 天创天辰联合分
 *
 * @author xj
 */

public class TcreditUnitedScoreParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 天创天辰联合分
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        TcreditUnitedScoreRecord obj = (TcreditUnitedScoreRecord) model;
        TcreditUnitedScoreData data = obj.getData();
        Integer status = data.getStatus();
        String message = data.getMessage();
        String seqNum = data.getSeqNum();
        String gid = data.getGid();
        String modelScore = data.getData() == null ? null : data.getData().getModelScore();
        String modelId = data.getData() == null ? null : data.getData().getModelId();

        StringBuilder sb = getBaseInfo(obj, successful);
        sb.append(status == null ? "" : status).append(CRT001);
        sb.append(message == null ? "" : message).append(CRT001);
        sb.append(seqNum == null ? "" : seqNum).append(CRT001);
        sb.append(gid == null ? "" : gid).append(CRT001);
        sb.append(modelScore == null ? "" : modelScore).append(CRT001);
        sb.append(modelId == null ? "" : modelId).append(CRT001);
        StringBuilder txSb = getBaseInfo(obj);
        key.set(getKey());
        val.set((sb.toString() + txSb).replace("\n", " ").replace("\r", " "));
        context.write(key, val);

        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tcredit_united_score_inc";
    }

    @Override
    public String getType() {
        return RecordType.TCREDIT_UNITED_SCORE_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "天创天辰联合分";
    }
}
